package cst.a921sxh.zyfypt_921sxh;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ResActivity extends AppCompatActivity {
    private EditText res_username;
    private EditText res_password;
    private EditText res_tel;
    private EditText res_email;
    private RadioButton rb_student;
    private RadioButton rb_teacher;
    private Button btn_res2;
    private ResModel model;
    private sxhListener<String> listener;
    String jsq=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_res);
        res_username=(EditText)findViewById(R.id.res_username);
        res_password=(EditText)findViewById(R.id.res_password);
        res_tel=(EditText)findViewById(R.id.res_tel);
        res_email=(EditText)findViewById(R.id.res_email);
        rb_student=(RadioButton)findViewById(R.id.rb_student);
        rb_teacher=(RadioButton)findViewById(R.id.rb_teacher);
        btn_res2=(Button)findViewById(R.id.btn_reg2);
        listener = new sxhListener<String>(){
            @Override
            public void onResponse(String l) {
                if(res_username.length()==0||res_password.length()==0||res_tel.length()==0||res_email.length()==0)
                {
                    Toast.makeText(ResActivity.this,"您的信息没填全",Toast.LENGTH_SHORT).show();
                }
                else if(Judgement.isEmail(res_email.getText().toString())&&Judgement.isTelephone(res_tel.getText().toString())) {
                    if (l.equals("1")) {
                        Toast.makeText(ResActivity.this, "恭喜您，注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(ResActivity.this, "用户名或密码已经注册过", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(ResActivity.this,"您的邮箱或者电话格式不对",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ResActivity.this,msg,Toast.LENGTH_SHORT).show();
            }

        };
        rb_student.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsq="2";
            }
        });
        rb_teacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsq="3";
            }
        });
        btn_res2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model=new ResModel("s");
                model.res(res_username.getText().toString(),res_password.getText().toString(),res_tel.getText().toString(),jsq,
                        res_email.getText().toString(),listener);
            }
        });
    }
}
